package week_01;

public class LinkedList {
    public class Node {
        int value;
        Node next;
        Node pre;
    }

    public Node head;
    public Node tail;

    public LinkedList() {
        head = new Node();
        tail = new Node();
        head.next = tail;
        tail.pre = head;
    }

    // 头插入
    public void insert(Node p, int val) {
        Node node = new Node();
        node.value = val;
        p.next.pre = node;
        node.next = p.next;
        node.pre = p;
        p.next = node;
    }

    public void remove(int value) {
        Node node = head.next;
        while (node != null && node != tail) {
            if (node.value == value) {
                node.next.pre = node.pre;
                node.pre.next = node.next;
                return;
            }
            node = node.next;
        }
    }

    public void remove(Node p) {
        p.pre.next = p.next;
        p.next.pre = p.pre;
    }

}
